package com.client.forms;


import java.util.HashMap;
import java.util.Map;
import com.client.controller.NumericController;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.shared.data.IData;
import com.shared.model.Tarification;

public class TarificationEditor extends DataEditor<Tarification>{
	
	
	
	private TextBox montantHonorairesTextBox = new TextBox();
	private TextBox montantCommissionsTextBox = new TextBox();
	private TextBox montantCourtageTextBox = new TextBox();
	private TextBox montantCompagnieTextBox = new TextBox();
	private TextBox taxeAssuranceTextBox = new TextBox();
	private TextBox fraisCourtageTextBox = new TextBox();
	private TextBox montantCommissionCATNATTextBox = new TextBox();
	
	
	public TarificationEditor() {
		edited = new Tarification();
		setSpacing(5);
		
		HorizontalPanel montantHonorairesPanel = new HorizontalPanel();
		montantHonorairesPanel.add(new Label("Montant Honoraires : "));
		montantHonorairesPanel.add(montantHonorairesTextBox);
		add(montantHonorairesPanel);
		listController.add(new NumericController(montantHonorairesTextBox, "Montant Honoraires"));
		
		HorizontalPanel montantCommissionsPanel = new HorizontalPanel();
		montantCommissionsPanel.add(new Label("Montant Commissions : "));
		montantCommissionsPanel.add(montantCommissionsTextBox);
		add(montantCommissionsPanel);
		listController.add(new NumericController(montantCommissionsTextBox, "Montant Commissions"));
		
		HorizontalPanel montantCourtagePanel = new HorizontalPanel();
		montantCourtagePanel.add(new Label("Montant Courtage : "));
		montantCourtagePanel.add(montantCourtageTextBox);
		add(montantCourtagePanel);
		listController.add(new NumericController(montantCourtageTextBox, "Montant Courtage"));
		
		HorizontalPanel montantCompagniePanel = new HorizontalPanel();
		montantCompagniePanel.add(new Label("Montant Compagnie : "));
		montantCompagniePanel.add(montantCompagnieTextBox);
		add(montantCompagniePanel);
		listController.add(new NumericController(montantCompagnieTextBox, "Montant Compagnie"));
		
		HorizontalPanel taxeAssurancePanel = new HorizontalPanel();
		taxeAssurancePanel.add(new Label("Taxe Assurance : "));
		taxeAssurancePanel.add(taxeAssuranceTextBox);
		add(taxeAssurancePanel);
		listController.add(new NumericController(taxeAssuranceTextBox, "Taxe Assurance"));
		
		HorizontalPanel fraisCourtagePanel = new HorizontalPanel();
		fraisCourtagePanel.add(new Label("Frais de Courtage : "));
		fraisCourtagePanel.add(fraisCourtageTextBox);
		add(fraisCourtagePanel);
		listController.add(new NumericController(fraisCourtageTextBox, "Frais de Courtage"));
		
		HorizontalPanel montantCommissionCATNATPanel = new HorizontalPanel();
		montantCommissionCATNATPanel.add(new Label("Montant Commission : "));
		montantCommissionCATNATPanel.add(montantCommissionCATNATTextBox);
		add(montantCommissionCATNATPanel);
		listController.add(new NumericController(montantCommissionCATNATTextBox, "Montant Commission"));
		
		setEmpty();
	}
	
	public void fill(){
		if(edited!=null){
			montantHonorairesTextBox.setValue(String.valueOf(edited.getMontantHonoraires()));
			montantCommissionCATNATTextBox.setValue(String.valueOf(edited.getMontantCommissionCATNAT()));
			montantCompagnieTextBox.setValue(String.valueOf(edited.getMontantCompagnie()));
			montantCourtageTextBox.setValue(String.valueOf(edited.getMontantCourtage()));
			taxeAssuranceTextBox.setValue(String.valueOf(edited.getTaxeAssurance()));
			fraisCourtageTextBox.setValue(String.valueOf(edited.getFraisCourtage()));
			montantCommissionsTextBox.setValue(String.valueOf(edited.getMontantCommissions()));
		}
	}
	
	
	
	public Tarification getEdited() throws IllegalArgumentException{
		check();
		edited.setMontantHonoraires(Double.parseDouble(montantHonorairesTextBox.getValue()));
		edited.setMontantCommissions(Double.parseDouble(montantCommissionsTextBox.getValue()));
		edited.setMontantCourtage(Double.parseDouble(montantCourtageTextBox.getValue()));
		edited.setMontantCompagnie(Double.parseDouble(montantCompagnieTextBox.getValue()));
		edited.setTaxeAssurance(Double.parseDouble(taxeAssuranceTextBox.getValue()));
		edited.setFraisCourtage(Double.parseDouble(fraisCourtageTextBox.getValue()));
		edited.setMontantCommissionCATNAT(Double.parseDouble( montantCommissionCATNATTextBox.getValue()));
		return edited;			
	}
	
	public Map<String,String> getEditedAsMap() throws IllegalArgumentException{
		check();
		Map<String,String> result = new HashMap<String, String>();
		result.put("montantHonoraires", montantHonorairesTextBox.getValue());
		result.put("montantCommissions", montantCommissionsTextBox.getValue());
		result.put("montantCourtage", montantCourtageTextBox.getValue());
		result.put("montantCompagnie", montantCompagnieTextBox.getValue());
		result.put("taxeAssurance", taxeAssuranceTextBox.getValue());
		result.put("fraisCourtage", fraisCourtageTextBox.getValue());
		result.put("montantCommissionCATNAT", montantCommissionCATNATTextBox.getValue());
		return result;
	}

	@Override
	public void setEmpty() {
		setEdited(new Tarification());
		fill();		
	}
	
	

}
